﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using ITAP.ShprossesBuilder.Operations;
using System.IO;

namespace ITAP.ShprossesBuilder.Editor
{
    public class CustomUserOperationsProvider
    {
        public static List<IOperation> LoadOperationsFromFile(string fileName)
        {
            // TODO Протестировать загрузку операций из файла
            List<IOperation> operations = new List<IOperation>();
            Assembly currentAssembly = Assembly.GetExecutingAssembly();
            string currentAssemblyLocation = Path.GetDirectoryName(currentAssembly.Location);
            try
            {

                string[] files = Directory.GetFiles(currentAssemblyLocation, "*.dll");

                foreach (string file in files)
                {
                    try
                    {
                        Assembly assembly = Assembly.LoadFile(file);
                        Type[] types = assembly.GetExportedTypes();
                        foreach (Type type in types)
                        {
                            object operation = Activator.CreateInstance(type, false);
                            if (operation is IOperation)
                                operations.Add(operation as IOperation);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                return operations;
            }
            catch
            {
                return operations;
            }
        }
    }
}
